﻿using System;
using System.Collections;
using System.Web.Mvc;
using Huirui.Cavan.Application.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SalesModule;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.Seedwork;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Huirui.Cavan.Presentation.Controller.SalesModule
{
	#region Receipt

    public class ReceiptController : EntityController<Receipt, string, ReceiptSearch, IReceiptViewModel, IReceiptAppService>, IReceiptController
    {
        public override string ControllerName
        {
            get { return ControllerConst.RECEIPT; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public ReceiptController()
        {
            this.Model = ModelFactory.CreateModel<IReceiptViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "receipt_query")]
        public ActionResult AddReceipt(string id)
        {
            if (id.IsNotEmpty())
            {
                this.Model.Item = this.AppService.GetById(id);
            }
            return View("~/Views/Sales/v1/addReceipt.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "receipt_edit")]
        public JsonResult AddPay(ReceiptViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.ReceiptDetails = JsonConvert.DeserializeObject<IList<ReceiptDetail>>(model.ReceiptDetails);

            BoolGenericResult<Receipt> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
	#endregion
}